package database;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	private JTextField codeIn;
	private JTextField eaIn;
	private JLabel label;
	private JLabel label_1;
	private JButton btnMod;
	private JLabel status;
	private JButton btnSearch;
	private JLabel lblSerial;
	JTextField serialIn;
	
	Connection conn;
	DatabaseTest dt;
	private JButton btnDel;

	public Update(Connection c, DatabaseTest d) {
		super("수정",true,true,true,false);
		setBounds(200, 0, 312, 200);
		getContentPane().setLayout(null);
		getContentPane().add(getCodeIn());
		getContentPane().add(getEaIn());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getBtnMod());
		getContentPane().add(getStatus());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getLblSerial());
		getContentPane().add(getSerialIn());
		getContentPane().add(getBtnDel());
		
		this.conn=c;
		this.dt=d;
	}

	private JTextField getCodeIn() {
		if (codeIn == null) {
			codeIn = new JTextField();
			codeIn.setColumns(10);
			codeIn.setBounds(70, 37, 102, 21);
		}
		return codeIn;
	}
	private JTextField getEaIn() {
		if (eaIn == null) {
			eaIn = new JTextField();
			eaIn.setColumns(10);
			eaIn.setBounds(70, 65, 102, 21);
		}
		return eaIn;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("Code");
			label.setBounds(12, 40, 46, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("Ea");
			label_1.setBounds(12, 68, 46, 15);
		}
		return label_1;
	}
	private JButton getBtnMod() {
		if (btnMod == null) {
			btnMod = new JButton("수정");
			btnMod.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					modify();
				}
			});
			btnMod.setBounds(184, 36, 102, 23);
		}
		return btnMod;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요.");
			status.setBounds(12, 146, 160, 15);
		}
		return status;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("조회");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					search();
				}
			});
			btnSearch.setBounds(184, 8, 102, 23);
		}
		return btnSearch;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("Serial");
			lblSerial.setBounds(12, 12, 46, 15);
		}
		return lblSerial;
	}
	private JTextField getSerialIn() {
		if (serialIn == null) {
			serialIn = new JTextField();
			serialIn.setColumns(10);
			serialIn.setBounds(70, 9, 102, 21);
		}
		return serialIn;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("삭제");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					delete();
				}
			});
			btnDel.setBounds(184, 65, 102, 23);
		}
		return btnDel;
	}
	
	
	
	public void setData(String se, String co, String ea){
		serialIn.setText(se);
		codeIn.setText(co);
		eaIn.setText(ea);
	}
	
	
	public void search(){
		try{
			if(serialIn.getText().trim().equals("")){
				status.setText("Serial을 입력해주세요.");
			}else{
				int sin = Integer.parseInt(serialIn.getText());
				String sql = "select * from ptbl where serial=?";
				
				PreparedStatement  pstmt = conn.prepareStatement(sql);
				
				pstmt.setInt(1, sin);
				
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()){
					codeIn.setText(rs.getString("code"));
					eaIn.setText(""+rs.getInt("ea")); // 숫자가 문자화 되어 들어갈 수 있도록 ""+ 써준다.
					
				}else{
					status.setText("찾는 데이터가 없습니다.");
					serialIn.setText("");
					codeIn.setText("");
					eaIn.setText("");
					serialIn.requestFocus();
					
				}						
				pstmt.executeUpdate();
				
				rs.close();
				pstmt.close();
				
				serialIn.setEnabled(false);
				
			}
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void modify(){
		try{
			int sin = Integer.parseInt(serialIn.getText());
			String cin = codeIn.getText();
			int ein = Integer.parseInt(eaIn.getText());
			
			String sql = "update ptbl set code=?, ea=? where serial=?";
			
			PreparedStatement pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, cin);
			pstmt.setInt(2, ein);
			pstmt.setInt(3, sin);
			
			pstmt.executeUpdate();
			pstmt.close();
			
			codeIn.setText("");
			eaIn.setText("");
			serialIn.setText("");
			
			status.setText("데이터가 수정되었습니다.");
			
			if(dt.select == null){
				dt.openSelect();
			}
			
			dt.select.searchAll();
			
			serialIn.setEnabled(true);
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void delete(){
		try{
			int sin = Integer.parseInt(serialIn.getText());						
			String sql = "delete from ptbl where serial=?";
			
			PreparedStatement pstmt = conn.prepareStatement(sql);
									
			pstmt.setInt(1, sin);
			
			pstmt.executeUpdate();
			pstmt.close();
			
			codeIn.setText("");
			eaIn.setText("");
			serialIn.setText("");
			
			status.setText("데이터가 삭제되었습니다.");
			serialIn.setEnabled(true);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
}
